{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% set canedit      = params['canedit'] ?? true %}
{% set withtemplate = params['withtemplate'] ?? '' %}
{% set rand         = rand|default(random()) %}
{% set nametype     = params['formtitle'] ?? item.getTypeName(1) %}
{% set friendlyname = params['friendlyname'] ?? item.getHeaderName() %}
{% set id           = item.fields['id'] ?? -1 %}
{% set in_navheader = in_navheader|default(false) %}
{% set header_toolbar = header_toolbar|default([]) %}
{% set entity_name = entity_name|default('') %}

{% if entity_id is not defined %}
    {% if item.getType() == 'Entity' %}
        {% set entity_id = item.fields['id'] == 0 ? null : (params['entities_id'] ?? item.fields['entities_id'] ?? session('glpiactive_entity')) %}
    {% elseif item.isEntityAssign() %}
        {% set entity_id = params['entities_id'] ?? item.getEntityID() ?? session('glpiactive_entity') %}
    {% else %}
        {% set entity_id = 0 %}
    {% endif %}
    {% if is_multi_entities_mode() %}
        {% set entity_name = entity_id is not null ? get_item_name('Entity', entity_id) : null %}
    {% endif %}
{% endif %}

{% if withtemplate == 2 and not item.isNewItem() %}
   {% set template_name = item.fields['template_name'] %}
   <input type="hidden" name="template_name" value="{{ template_name }}" />
   {% set nametype = __('Created from the template %s')|format(template_name) %}
{% elseif withtemplate == 1 %}
   {% set template_name = item.fields['template_name'] %}
   <input type="hidden" name="is_template" value="1" />
{% elseif item.isNewItem() %}
   {% set nametype = __('%1$s - %2$s')|format(__('New item'), nametype) %}
{% endif %}

{% if item.canEdit(item.fields['id']) %}
   <form name="massaction_{{ rand }}" id="massaction_{{ rand }}" method="post"
         action="{{ path('/front/massiveaction.php') }}?_single_item[itemtype]={{ item.getType() }}&_single_item[id]={{ item.getID() }}" data-submit-once>
      <div id="massive_container_{{ rand }}"></div>
      <input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}" />
   </form>
{% endif %}

<div id="header_{{ rand }}"
     class="card-header main-header d-flex flex-wrap me-2 mt-n2 align-items-stretch {% if in_navheader %} align-self-end {% endif %} flex-grow-1">
   {% if withtemplate == 1 and item.isNewItem() %}
      <input type="text" class="form-control ms-4 mb-2" placeholder="{{ __('Template name') }}"
             name="template_name" id="textfield_template_name{{ rand }}"
             value="{{ template_name }}" />
   {% endif %}
   <h3 class="card-title d-flex align-items-center {{ in_navheader ? "" : "ps-5" }}">
      {% set icon = item.getIcon() %}
      {% if not in_navheader and icon|length > 0 %}
         <div class="ribbon ribbon-bookmark ribbon-top ribbon-start bg-blue s-1">
            <i class="{{ icon }} fa-2x"></i>
         </div>
      {% endif %}
      <span {% if in_navheader %} class="status rounded-1 me-1" {% endif %}>
         {% if in_navheader %}
            <i class="{{ icon }}"></i>
         {% endif %}
         {% if withtemplate == 1 and item.id > 0 %}
            {{ _n('Template', 'Templates', 1) }} - {{ nametype }} - {{ template_name }}
         {% elseif not item.isNewItem() %}
            {{ nametype }} - <span id="header-friendlyname"> {{ friendlyname }} </span>
         {% else %}
            {{ nametype }}
         {% endif %}
      </span>
      {% if in_navheader and item.isField('is_dynamic') and item.fields['is_dynamic'] %}
      <span class="mx-1 status rounded-1" title="{{ __('Automatic Inventory') }}" data-bs-toggle="tooltip">
         <i class="{{ call('Agent::getIcon') }}"></i>
      </span>
      {% endif %}
      {% if in_navheader and item.isField('is_deleted') %}
      {% set title = item.isField('date_mod') ? __('Item has been deleted on %s')|format(item.fields['date_mod']) : __('Deleted') %}
      <span
         id="header-is-deleted"
         class="mx-1 status rounded-1"
         title="{{ title }}"
         data-bs-toggle="tooltip"
      >
         <i class="ti ti-trash"></i>
         {{ __('Deleted') }}
      </span>
      {% endif %}

      {% if header_toolbar|length > 0 %}
         <div class="d-inline-block toolbar ms-2">
            {% for raw_element in header_toolbar %}
               {{ raw_element|raw }}
            {% endfor %}
         </div>
      {% endif %}
   </h3>

   {% set single_actions_ms_auto = true %}
   {% if item.isEntityAssign() and is_multi_entities_mode() and item is not instanceof('Entity') %}
      {% set single_actions_ms_auto = false %}
      <span class="badge entity-name mx-1 px-2 ms-auto align-items-center" title="{{ entity_name }}">
                  <i class="ti ti-stack me-2"></i>
                  {{ entity_name }}
               </span>

      {% if item.maybeRecursive() %}
         <span class="badge is_recursive-toggle mx-1 px-2 align-items-center">
            <label class="form-check d-flex align-items-center mb-0">
               {% set disabled = canedit == false %}
               {% set comment  = __('Change visibility in child entities.') %}

               {% if item is instanceof('CommonDBChild') %}
                  {% set comment  = __('Can՛t change this attribute. It՛s inherited from its parent.') %}
                  {% set disabled = true %}
               {% elseif not item.canRecurs() %}
                  {% set comment  = __('You are not allowed to change the visibility flag for child entities.') %}
                  {% set disabled = true %}
               {% elseif not item.canUnrecurs() %}
                  {% set comment  = __('Flag change forbidden. Linked items found.') %}
                  {% set disabled = true %}
               {% endif %}

               {% if not disabled %}<input form="main-form" type="hidden" name="is_recursive" value="0" />{% endif %}
               <input form="main-form" class="form-check-input" type="checkbox" name="is_recursive" value="1"
                  {% if item.isRecursive() %}checked="checked"{% endif %}
                  {% if disabled %}disabled="disabled"{% endif %} />
               {% if item is instanceof('CommonDBChild') and item.isNewItem() and item.isRecursive() %}
                  {# Send value on hidden field to ensure creation will use inherited recursivity on CommonDBChild #}
                  <input form="main-form" type="hidden" name="is_recursive" value="1" />
               {% endif %}
               <span class="form-check-label mb-0 mx-2">
                  {{ __('Child entities') }}
                  <i class="ti ti-info-circle ms-1" title="{{ comment }}"></i>
               </span>
            </label>
         </span>
      {% endif %}
   {% endif %}

   {{ include('components/form/single-action.html.twig') }}
</div>
